package com.spaceraccoons.api.adapter;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalTime;

import com.spaceraccoons.api.types.DateTimeProtos;

/**
 * 
 * @author Benjamin P. Jung
 */
public class TimeAdapter extends ProtobufAdapter<DateTimeProtos.Time, LocalTime> {

	@Override
	public LocalTime unmarshal(final DateTimeProtos.Time pbTime) {

		final DateTime dateTime = new DateTime(
				1970, 1, 1,
				pbTime.getHourOfDay(),
				pbTime.getMinuteOfHour(),
				pbTime.getSecondOfMinute(),
				pbTime.hasMillisOfSecond() ? pbTime.getMillisOfSecond() : 0,
				DateTimeZone.forID(pbTime.getTimeZone()));
		
		return dateTime.toLocalTime();

	}

	@Override
	public DateTimeProtos.Time marshal(final LocalTime time) {

		return DateTimeProtos.Time.newBuilder()
				.setHourOfDay(time.getHourOfDay())
				.setMinuteOfHour(time.getMinuteOfHour())
				.setSecondOfMinute(time.getSecondOfMinute())
				.setMillisOfSecond(time.getMillisOfSecond())
				.setTimeZone(time.getChronology().getZone().getID())
				.build();

	}

}
